package com.tianqu.tms.www.proxy.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.WeChatMqApi;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000430Bo;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferProcessHistoryDaoImp;
import com.tianqu.tms.dto.BaseCarInfoDto;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.dto.TransferProcessHistoryDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.proxy.actionform.CarProxySearchInputActionForm;

import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferDataSyn000430;

public class CarProxyInputConfirmController extends TmsController {
	private Logger logger = Logger
			.getLogger(CarProxyInputConfirmController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			CarProxySearchInputActionForm inputForm = (CarProxySearchInputActionForm) uc.getInputForm();
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			TransferAcceptedInfoDto dto = new TransferAcceptedInfoDto();
			dto.setVin(inputForm.getVin());
			dto.setRegDateStr(inputForm.getRegDate());
			dto.setVendorCode(inputForm.getVendorCode());
			dto.setModelCode(inputForm.getModelCode());
			if(StringUtil.isNotBlank(inputForm.getCardType())) dto.setCy_cardType(new Integer(inputForm.getCardType()));
			dto.setCountryStyle(inputForm.getCountryStyle());
			dto.setCarUse(inputForm.getCarUse());
			if(StringUtil.isNotBlank(inputForm.getCarType())) dto.setCy_carType(inputForm.getCarType());
			dto.setRegisterCardNo(inputForm.getRegisterCardNo());
			dto.setAttachMemberId(inputForm.getMemberId());
			//原车组判断
			if(StringUtil.isNotBlank(inputForm.getCarOwner()) && inputForm.getCarOwner().length() >=5) dto.setOwnerType(2);
			else dto.setOwnerType(1);
			dto.setSerial(inputForm.getSerial());
			dto.setCarOwner(inputForm.getCarOwner());
			dto.setOwnerPhone(inputForm.getOwnerPhone());
			dto.setOwnerAddress(inputForm.getOwnerAddress());
			dto.setAgentOwnerIdCardNo(inputForm.getAgentOwnerIdCardNo());
			dto.setAgentName(inputForm.getAgentName());
			dto.setAgentPhone(inputForm.getAgentPhone());
			dto.setAgentAddress(inputForm.getAgentAddress());
			dto.setMaterialImages1(inputForm.getMaterialImages1());
			dto.setMaterialImages2(inputForm.getMaterialImages2());
			dto.setMaterialImages3(inputForm.getMaterialImages3());
			dto.setMaterialImages4(inputForm.getMaterialImages4());
			dto.setMaterialImages5(inputForm.getMaterialImages5());
			dto.setMaterialImages6(inputForm.getMaterialImages6());
			dto.setMaterialImages7(inputForm.getMaterialImages7());
			dto.setMaterialImages8(inputForm.getMaterialImages8());
			dto.setMaterialImages9(inputForm.getMaterialImages9());
			dto.setMaterialImages10(inputForm.getMaterialImages10());
			dto.setMemberId(inputForm.getMemberId());
			TmsUserProfile profile = uc.getUserProfile();
			dto.setInsertUserid(profile.getUniqueId());
			dto.setUpdateUserId(profile.getUniqueId());
			accept.updateMarketTypeBySerial(dto);
			
			//
			// 插入 过户流程操作记录表（tms_transfer_process_history）
			TransferProcessHistoryDto transferProcessHistoryDto = new TransferProcessHistoryDto();
			transferProcessHistoryDto.setBusinessType(13);
			transferProcessHistoryDto.setInsertUserId(profile.getUniqueId());
			transferProcessHistoryDto.setSerial(inputForm.getSerial());
			TransferProcessHistoryDaoImp transferProcessHistoryDao = (TransferProcessHistoryDaoImp) TransferProcessHistoryDaoImp.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
			transferProcessHistoryDao.insertHisInfoBySerial(transferProcessHistoryDto);
			
			//
			Car car = new Car();
			car = car.findCarBaseInfoByVin(inputForm.getVin());
			BaseCarInfoDto baseCarInfoDto = new BaseCarInfoDto();
			baseCarInfoDto.setVin(inputForm.getVin());
			if(StringUtil.isNotBlank(inputForm.getCountryStyle())) baseCarInfoDto.setCountryStyle(Integer.parseInt(inputForm.getCountryStyle()));
			if(StringUtil.isNotBlank(inputForm.getCarUse())) baseCarInfoDto.setCarUse(Integer.parseInt(inputForm.getCarUse()));
			if(StringUtil.isNotBlank(inputForm.getCardType())) baseCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
			baseCarInfoDto.setRegisterCardNo(inputForm.getRegisterCardNo());
			baseCarInfoDto.setCarOwner(inputForm.getCarOwner());
			baseCarInfoDto.setOwnerAddress(inputForm.getOwnerAddress());
			baseCarInfoDto.setOwnerPhone(inputForm.getOwnerPhone());
			baseCarInfoDto.setCarOwnerIdCardNo(inputForm.getCarOwnerIdCardNo());
			baseCarInfoDto.setModelCode(inputForm.getModelCode());
			baseCarInfoDto.setVendorCode(inputForm.getVendorCode());
			if(StringUtil.isNotBlank(inputForm.getOwnerType())) baseCarInfoDto.setOwnerType(Integer.parseInt(inputForm.getOwnerType()));
			baseCarInfoDto.setCarOwnerIdCardNo(inputForm.getCarOwnerIdCardNo());
			if(StringUtil.isNotBlank(inputForm.getCarType())) baseCarInfoDto.setCarType(Integer.parseInt(inputForm.getCarType()));
			if(StringUtil.isNotBlank(inputForm.getCarOwner())){
				if(inputForm.getCarOwner().length() >=5) baseCarInfoDto.setOwnerType(2);
				else baseCarInfoDto.setOwnerType(1);
			} 
			TransferAcceptedInfo transferAcceptedInfo = accept.findAcceptInfoByVin(accept.getVin());
			if(transferAcceptedInfo!=null && transferAcceptedInfo.getIsMatch() == 2){
				baseCarInfoDto.setCarName(transferAcceptedInfo.getCarName());
				baseCarInfoDto.setVendorId(transferAcceptedInfo.getVendorId());
				baseCarInfoDto.setBrandId(transferAcceptedInfo.getBrandId());
				baseCarInfoDto.setCarStyleId(transferAcceptedInfo.getCarStyleId());
			}
			if(car ==null ){//insert
				Car.getInstance().insertBaseCarInfo(baseCarInfoDto);
			}else{//update
				Car.getInstance().updateBaseCarInfo(baseCarInfoDto);
			}
			
			
			//
			MarketCarInfoDto marketCarInfoDto = new MarketCarInfoDto();
			if(StringUtil.isNotBlank(inputForm.getCarOwnerIdCardNo())) {
				marketCarInfoDto = new MarketCarInfoDto();
				marketCarInfoDto.setCarOwner(inputForm.getCarOwner());
				marketCarInfoDto.setOwnerPhone(inputForm.getOwnerPhone());
				marketCarInfoDto.setOwnerAddress(inputForm.getOwnerAddress());
				marketCarInfoDto.setOwnerCardNo(inputForm.getCarOwnerIdCardNo());
				if(StringUtil.isNotBlank(inputForm.getCarOwner())) {
					if(inputForm.getCarOwner().length() >=5) marketCarInfoDto.setOwnerType(2);
					else marketCarInfoDto.setOwnerType(1);
				}
				if(StringUtil.isNotBlank(inputForm.getCardType())) marketCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
				List<MarketCarInfoDto> r = Car.getInstance().findCarOwnerByCardNo(inputForm.getCarOwnerIdCardNo());
				if(r.size() == 0){//insert
					Car.getInstance().insertCarOwner(marketCarInfoDto);
				}else{
					Car.getInstance().updateCarOwnerByOwnerCardNo(marketCarInfoDto);
				}
			}
			
			if(StringUtil.isNotBlank(inputForm.getAgentOwnerIdCardNo())) {
				marketCarInfoDto = new MarketCarInfoDto();
				marketCarInfoDto.setCarOwner(inputForm.getAgentName());
				marketCarInfoDto.setOwnerPhone(inputForm.getAgentPhone());
				marketCarInfoDto.setOwnerAddress(inputForm.getAgentAddress());
				marketCarInfoDto.setOwnerCardNo(inputForm.getAgentOwnerIdCardNo());
				marketCarInfoDto.setOwnerType(1);
				if(StringUtil.isNotBlank(inputForm.getCardType())) marketCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
				List<MarketCarInfoDto> r = Car.getInstance().findCarOwnerByCardNo(inputForm.getAgentOwnerIdCardNo());
				if(r.size() == 0){//insert
					Car.getInstance().insertCarOwner(marketCarInfoDto);
				}else{
					Car.getInstance().updateCarOwnerByOwnerCardNo(marketCarInfoDto);
				}
			}
			
			controller.commit();
			
			//API 430
			TransferDataSyn000430Bo bo = new TransferDataSyn000430Bo();
			bo.setSerial(inputForm.getSerial());
			bo.setVin(inputForm.getVin());
			bo.setVendorCode(inputForm.getVendorCode());
			bo.setModelCode(inputForm.getModelCode());
			bo.setCarType(Integer.parseInt(inputForm.getCarType()));
			bo.setCountryStyle(Integer.parseInt(inputForm.getCountryStyle()));
			bo.setCarUse(Integer.parseInt(inputForm.getCarUse()));
			bo.setCardType(Integer.parseInt(inputForm.getCardType()));
			bo.setRegiesterCardNo(inputForm.getRegisterCardNo());
			bo.setRegDate(inputForm.getRegDate());
			bo.setAttachMemberId(inputForm.getMemberId());
			bo.setOwnerType(Integer.parseInt(inputForm.getOwnerType()));
			bo.setOwnerOrgNo(inputForm.getOwnerOrgNo());
			bo.setCarOwnerIdCardNo(inputForm.getCarOwnerIdCardNo());
			bo.setCarOwner(inputForm.getCarOwner());
			bo.setOwnerPhone(inputForm.getOwnerPhone());
			bo.setOwnerAddress(inputForm.getOwnerAddress());
			bo.setAgentOwnerIdCardNo(inputForm.getAgentOwnerIdCardNo());
			bo.setAgentName(inputForm.getAgentName());
			bo.setAgentPhone(inputForm.getAgentPhone());
			bo.setAgentAddress(inputForm.getAgentAddress());
			bo.setMaterialImages1(inputForm.getMaterialImages1());
			bo.setMaterialImages2(inputForm.getMaterialImages2());
			bo.setMaterialImages3(inputForm.getMaterialImages3());
			bo.setMaterialImages4(inputForm.getMaterialImages4());
			bo.setMaterialImages5(inputForm.getMaterialImages5());
			bo.setMaterialImages6(inputForm.getMaterialImages7());
			bo.setMaterialImages7(inputForm.getMaterialImages7());
			bo.setMaterialImages8(inputForm.getMaterialImages8());
			bo.setMaterialImages9(inputForm.getMaterialImages9());
			bo.setMaterialImages10(inputForm.getMaterialImages10());
			bo.setCatClass(inputForm.getFromType());
			bo.setVendorId(inputForm.getVendorId());
			bo.setBrandId(inputForm.getBrandId());
			bo.setCarStyleId(inputForm.getCarStyleId());
			bo.setIsMatch(1);
//			bo.setOwnerSex(inputForm.getOwnerSex()); //TODO
//			bo.setOwnerBirth(inputForm.getOwnerBirth()); //TODO
			bo.setMarketNo(TmsConstants.MARKET_NO);
			bo.setCarName(inputForm.getCarName());
			WeChatMqApi.getInstance().call(bo);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	/**
	 * 处理存入数据库中的图片路径
	 */
	private String imgPath(String serial, String imgKey) {
		return ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY) + serial + "/" + serial + "_" + imgKey + ".jpg");
	}
}